#pragma once
#include <d3dx9.h>


// a generic light. It just has a colour and an assosiated D3D light object
class Light
{
public:
	Light()
	{
		Init();
		SetColour(1.0f,1.0f,1.0f);
	}

	Light(D3DXCOLOR color)
	{
		Init();
		SetColour(color);
	}

	//	~Light(void);

	void SetColour(float r, float g, float b)
	{
		light.Diffuse.r = r;
		light.Diffuse.g = g;
		light.Diffuse.b = b;
		light.Specular.r = r;
		light.Specular.g = g;
		light.Specular.b = b;
	}

	void SetColour(D3DXCOLOR color)
	{
		SetColour(color.r,color.g,color.b);
	}

	D3DXVECTOR3 vecDir;
	D3DLIGHT9 light;

private:
	void Init()
	{
		// set everything to zero
		ZeroMemory( &light, sizeof( D3DLIGHT9 ) );

		// unlimited range
		light.Range = 10000.0f;
	}
};

// Directional light is used to simulate distant light sources like the sun. 
//It has a direction but no position.
class DirectionalLight : 
	public Light
{
public:
	DirectionalLight(D3DXVECTOR3 direction)
		:Light()
	{
		Init(direction);
	}

	DirectionalLight(D3DXVECTOR3 direction, D3DXCOLOR color) 
		: Light(color)
	{
		Init(direction);
	}

private:
	void Init(D3DXVECTOR3 direction)
	{
		// direction MUST be normalised!
		D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &direction );

		//D3D light type
		light.Type = D3DLIGHT_DIRECTIONAL;
	}
};

// A Pointlight has a position in the world and radiates light in all directions. 
// A bare bulb on a stand would be an example of this
class PointLight :
	public Light
{
public:
	PointLight(D3DXVECTOR3 position) :
	  Light()
	  {
		  Init(position);
	  }

	  PointLight(D3DXVECTOR3 position, D3DXCOLOR color) :
	  Light(color)
	  {
		  Init(position);
	  }

private:
	void Init(D3DXVECTOR3 position)
	{
		light.Position = position;

		//D3D light type
		light.Type = D3DLIGHT_POINT;

		// A point light has an attenuation (how the light level decreases over distance)
		light.Attenuation0 = 0.1f;
		// and a range..
		light.Range=200.0f;
	}
};

//A spot light has a position in space and a direction. 
//It emits a cone of light with two degrees of intensity. It has a central brightly lit section 
//and a surrounding dimly lit section that merges with the surrounding shadow. 
//Only objects within the light cone are illuminated.
class SpotLight :
	public Light
{
public:
	SpotLight(D3DXVECTOR3 position, D3DXVECTOR3 direction) :
	  Light()
	  {
		  Init(position,direction);
	  }

	  SpotLight(D3DXVECTOR3 position, D3DXVECTOR3 direction, D3DXCOLOR color) :
	  Light(color)
	  {
		  Init(position,direction);
	  }

private:
	void Init(D3DXVECTOR3 position, D3DXVECTOR3 direction)
	{
		// spotlights have position and direction
		light.Position = position;

		// direction MUST be normalised!
		D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &direction );

		//D3D light type
		light.Type = D3DLIGHT_SPOT;

		// spot light defaults
		light.Range=100.0f;
		light.Theta=0.5f;
		light.Phi=1.0f;
		light.Falloff=1.0f;
		light.Attenuation0= 1.0f;
	}
};
